#include <stdlib.h>
#include <stdio.h>
  int* array;
int n;



void swap(int *i, int *j)
{
  int temp;

  temp = *i;
  *i = *j;
  *j = temp;
}





append(){
	printf("enter the number to be implemented : ");
	int app;
	scanf("%d", &app);
	n++;
	array=realloc(array,(n+1)* sizeof(*array));
	array[n-1]=app;
	}



print(){int i;
	for(i=0; i<n; i++)
		printf("%d, ", array[i]);
	}


insert(){int pos, num,i;
	printf("enter the position :");
	scanf("%d", &pos);		
	printf("enter the number :");
	scanf("%d", &num);		

	++n;
	array=realloc(array,(n)* sizeof(*array));
	for(i=n;i!=pos-1;--i )
		{
		array[i-1]=array[i-2];
		}
	array[pos-1]=num;
	printf("n = %d\n", n);
	}



remov(){int num,ii,j;
fflush(stdin);
	printf("enter the number :");
	scanf("%d", &num);		

	for(ii=0;ii<n;ii++ )
		{
		if(array[ii]==num)
	{		for (j=ii; j<n-1; j++)
			{swap(&array[j], &array[j+1]);}
			
			break;	
	}	}

--n;
	array=realloc(array,(n)* sizeof(*array));}







int pop(){int num, numb, ii, j;
	printf("enter the position(starts from 0) :");
	scanf("%d", &numb);		

printf("%d", array[numb]);

num = array[numb];
	for(ii=0;ii<n;ii++ )
		{
		if(array[ii]==num)
	{		for (j=ii; j<n-1; j++)
			{swap(&array[j], &array[j+1]);}
			
			break;	
	}	}

--n;
	array=realloc(array,(n)* sizeof(*array));
	return num;}







extend(){int num,ii, app;
	printf("enter the size of new list :");
	scanf("%d", &num);		
printf("enter the numbers :");
	for(ii=0;ii<num;ii++ )
		{
			scanf("%d", &app);
	n++;
	array=realloc(array,(n+1)* sizeof(*array));
	array[n-1]=app;

}
	}





sort(){int num,ii,kk, app;
	for(ii = 0; ii < n; ii++)
	for(kk = 0; kk < n-1; kk++)
		if(array[kk] > array[kk+1]) {
        	app = array[kk+1];
        	array[kk+1] = array[kk];
        	array[kk] = app;
      					}
	}






inde(){int num,ii, kk;
fflush(stdin);
	printf("enter the number :");
	scanf("%d", &num);		

	for(ii=0;ii<n;ii++ )
		{
		if(array[ii]==num)
	{		printf("%d position!", ii );
			
			break;	
	}	

}
	}





rev(){int num,ii, kk;
	kk=0;

	for(ii=0;ii<n/2;ii++ )
		{
		swap(&array[ii], &array[n-1-ii]);
		}
	}


count(){int num,ii, kk;
	printf("enter the number :");
	scanf("%d", &num);		
kk=0;
	for(ii=0;ii<n;ii++ )
		{
		if(array[ii]==num)	kk++;
		}
	printf("%d appears %d times", num, kk);}




int main() {

  int i;
  printf("Enter the number of elements: ");
  scanf("%d", &n);

  array = (int*) malloc(n*sizeof(int));
  for (i=0; i<n; i++) {
    printf("Enter number %d: ", i);
    scanf("%d", &array[i]);
  }
  printf("\nThe Dynamic Array is: \n");
  for (i=0; i<n; i++) {
    printf("The value of %d is %d\n", i, array[i]);
  }
  printf("Size= %d\n", i);

int inp;
for(;;){	

		printf("\n ~~~~menu~~~~\n");
		printf("\n 1. append\n");
		printf("2. print\n");
		printf("3. insert\n");
		printf("4. delete\n");
		printf("5. index\n");
		printf("6. count\n");
		printf("7. reverse\n");
		printf("8. pop\n");
		printf("9. extend\n");
		printf("10. sort\n");
		scanf("%d", &inp);

		if (inp == 1)	append();
		if (inp == 2)	print();
		if (inp == 3)	insert();
		if (inp == 4)	remov();
		if (inp == 5)	inde();
		if (inp == 6)	count();
		if (inp == 7)	rev();
		if (inp == 8)	pop();
		if (inp == 9)	extend();
		if (inp == 10)	sort();
		if (inp == 0)   exit(1);
	}
}


